import settings
from social.utils.celery_app import app
from social.utils.jpush import JPush


@app.task()
def send_post_notification(notification_id):
    from social.notification.models import Notification
    from social.user.models import UserMsgSetting
    notification = Notification.objects.select_related('user').get(id=notification_id)
    msg_setting, _ = UserMsgSetting.objects.get_or_create(user=notification.user)
    can_post_like = msg_setting.post_like and notification.type == Notification.TYPE_LIKE
    can_post_comment = msg_setting.post_comment and notification.type == Notification.TYPE_COMMENT
    if can_post_like or can_post_comment:
        JPush.send_push([notification.user.hid], settings.APP_TITLE, notification.content, alert=notification.content,
                        content_type='post_msg',
                        extra={
                            'link': f'{settings.APP_NAME}://notification',
                            'title': settings.APP_TITLE,
                            'content': notification.content
                        }, channel_id='post')
